package battleship;
/* Client.java */  
import java.io.BufferedReader;   
import java.io.File;
import java.io.FileReader;
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.io.PrintWriter;   
import java.net.Socket;   
  
public class Klient   
{   
	 private BufferedReader read; 
	 private File f;   
	
    private int PORT = 0;   
    private String HOST = "";   
       
    Klient() throws IOException{
        Socket socket = null;   
        f = new File("Configuration.txt");
        read = new BufferedReader(new FileReader(f));
        HOST = read.readLine();
        PORT = Integer.parseInt(read.readLine());
        try  
        {   
            socket = new Socket(HOST, PORT);   
        }   
        catch(Exception e)   
        {   
            System.err.println("Could not connect to "+HOST+":"+PORT);   
            System.exit(1);   
        }   
           
        final PrintWriter out = new PrintWriter(socket.getOutputStream(),true);   
        final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
           
        Thread t = new Thread(new Runnable()   
        {   
            public void run()   
            {   
                long start = System.currentTimeMillis();   
                   
                while (true)   
                {   
                    try  
                    {   
                        String input = in.readLine();   
  
                        if (input != null)   
                        {   
                            System.out.println(System.currentTimeMillis() + " Server: " + input);   
                        }   
  
                        if (input.equals("Ping"))   
                        {   
                            if(System.currentTimeMillis()-start>30000)   
                            {   
                                out.println("Pon g");   
                                System.out.println(System.currentTimeMillis() + " Client: Pon g");   
                                break;   
                            }   
                               
                            out.println("Pong");   
                            System.out.println(System.currentTimeMillis() + " Client: Pong");   
                        }   
                    }   
                    catch (IOException ioe)   
                    {   
                        //   
                    }   
                }   
            }   
        });   
        t.start();   
           
        out.close();   
        in.close();   
        socket.close();   
    }   
}  
